package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.RecommendVideo;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class VideoServiceImpl implements VideoService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //因为redisId自增器生成的是专门给大数据团队用的,所以写在service层中
    @Autowired
    private RedisIDService redisIDService;
    //查询视频列表
    @Override
    public PageBeanVo findVideoByPage(Long userId, Integer pageNum, Integer pageSize) {
        //分页查询，根据缘分值降序
        Integer index = (pageNum - 1)*pageSize ;
        Query query = new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("score")))
                .skip(index).limit(pageSize);
        List<RecommendVideo> recommendVideos = mongoTemplate.find(query, RecommendVideo.class);

        ArrayList<Video> videos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(recommendVideos)) {
            for (RecommendVideo recommendVideo : recommendVideos) {
                Video video = mongoTemplate.findById(recommendVideo.getVideoId(), Video.class);
                videos.add(video);
            }
        }

        long count = mongoTemplate.count(query, RecommendVideo.class);
        return new PageBeanVo(pageNum,pageSize,count,videos);
    }

    @Override
    public void saveVideo(Video video) {
        //封装vid
        Long vid = redisIDService.getNextId(ConstantUtil.VIDEO_ID);
        video.setVid(vid);
        //保存
        mongoTemplate.save(video);
        //R_V中添加
        RecommendVideo recommendVideo = new RecommendVideo();
        recommendVideo.setDate(video.getCreated());//推荐时间，用当前时间或者发布时间都行
        recommendVideo.setVideoId(video.getId());
        recommendVideo.setVid(vid);
        recommendVideo.setScore(100D);
        //推荐给自己
        recommendVideo.setUserId(video.getUserId());
        mongoTemplate.save(recommendVideo);
    }

    @Override
    public PageBeanVo findVideoList(Long userId, Integer pageNum, Integer pageSize) {
        Integer index =(pageNum  - 1)*pageSize;
        Query query = new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip(index).limit(pageSize);
        List<Video> videoList = mongoTemplate.find(query, Video.class);

        long count = mongoTemplate.count(query, Video.class);
        return  new PageBeanVo(pageNum,pageSize,count,videoList);
    }
}
